<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:patentSearch:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="patentSearchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55" align="center" type="index" />
      <el-table-column label="专利名称" align="left" prop="name" show-overflow-tooltip>
				<template #default="scope">
					<el-button link type="primary" @click="handleUpdate(scope.row)" >{{scope.row.name}}</el-button>
				</template>
			</el-table-column>
      <el-table-column label="专利来源" align="left" prop="patentResource" show-overflow-tooltip/>
      <el-table-column label="专利类型" align="left" prop="patentType" show-overflow-tooltip/>
      <el-table-column label="专利描述" align="left" prop="patentDescription" show-overflow-tooltip/>
      <el-table-column label="网址" align="left" prop="website" show-overflow-tooltip/>
      <template #empty>
        <el-empty />
      </template>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专利查询对话框 -->
    <el-dialog :title="title" v-model="open" width="60%" draggable append-to-body>
      <el-form ref="patentSearchRef" :model="form" :rules="rules" label-width="80px">
				<el-row>
					<el-col :span="12">
						<el-form-item label="专利名称" prop="name">
						  <el-input v-model="form.name" placeholder="请输入专利名称" />
						</el-form-item>
					</el-col>
					<el-col :span="12">	
						<el-form-item label="专利类型" prop="patentType">
						  <el-input v-model="form.patentType" placeholder="专利类型（发明、实用新型和外观设计、著作）" />
						</el-form-item>
					</el-col>
				</el-row>
				
				<el-row>
					<el-col :span="12">
						<el-form-item label="专利网址" prop="website">
						  <el-input v-model="form.website" placeholder="请输入专利网址" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="专利来源" prop="patentResource">
						  <el-input v-model="form.patentResource" placeholder="请输入专利来源(国内 | 国外)" />
						</el-form-item>
					</el-col>
				</el-row>
        <el-form-item label="专利描述" prop="patentDescription">
          <el-input v-model="form.patentDescription" type="textarea" :rows="15" placeholder="请输入500字内的专利描述" show-word-limit />
        </el-form-item>
        
        <el-form-item label="附件" prop="files">
          <file-upload v-model="form.files"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  
	<!-- 专利查询网站 -->
	<div class="patent-sites-section">
	  <h2 class="section-title">常用专利查询网站</h2>
	  
	  <div class="search-filter">
	    <el-input
	      v-model="searchText"
	      placeholder="搜索专利查询网站..."
	      class="search-input"
	      :prefix-icon="Search"
	      clearable
	    />
	    <el-radio-group v-model="filterType" class="filter-group">
	      <el-radio-button label="all">全部</el-radio-button>
	      <el-radio-button label="domestic">国内</el-radio-button>
	      <el-radio-button label="international">国际</el-radio-button>
	      <el-radio-button label="recommended">推荐</el-radio-button>
	    </el-radio-group>
	  </div>
	  
	  <div class="patent-sites-grid">
	    <div 
	      v-for="site in filteredSites" 
	      :key="site.id"
	      class="site-card"
	    >
	      <div class="card-header">
	        <div class="site-icon" :style="{ backgroundColor: site.color }">
	          {{ site.icon }}
	        </div>
	        <div class="site-name">{{ site.name }}</div>
	      </div>
	      
	      <div class="card-content">
	        <div class="site-url">{{ formatUrl(site.url) }}</div>
	        <div class="site-desc">{{ site.description }}</div>
	      </div>
	      
	      <div class="card-footer">
	        <el-button type="primary" @click="visitSite(site.url)" size="small" class="visit-button">访问网站</el-button>
	        <span v-if="site.recommended" class="recommend-tag">推荐</span>
	      </div>
	    </div>
	  </div>
	</div>
	
	</div>
</template>

<script setup name="PatentSearch">
import { listPatentSearch, getPatentSearch, delPatentSearch, addPatentSearch, updatePatentSearch } from "@/api/business/patentSearch"
import { useRoute } from 'vue-router'
import { Search } from '@element-plus/icons-vue'

const route = useRoute()
const { proxy } = getCurrentInstance()

const patentSearchList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

// 专利查询网站相关
const searchText = ref('')
const filterType = ref('all')

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: route.query.id,
    name: null,
    patentResource: null,
    patentType: null,
  },
  rules: {
    patentType: [
      { required: true, message: "专利类型不能为空", trigger: "blur" }
    ],
		name: [
		  { required: true, message: "专利名称不能为空", trigger: "blur" }
		],
		patentDescription: [
		  { required: true, message: '专利描述不能为空', trigger: 'blur' },
		  { max: 500, message: '专利描述不能超过500字', trigger: 'blur' }
		]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询专利查询列表 */
function getList() {
  loading.value = true
  listPatentSearch(queryParams.value).then(response => {
    patentSearchList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    projectId: route.query.id,
    name: null,
    patentResource: null,
    patentType: null,
    patentDescription: null,
    website: null,
    files: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("patentSearchRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加专利查询"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPatentSearch(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改专利查询"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["patentSearchRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePatentSearch(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPatentSearch(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除专利查询编号为"' + _ids + '"的数据项？').then(function() {
    return delPatentSearch(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/patentSearch/export', {
    ...queryParams.value
  }, `patentSearch_${new Date().getTime()}.xlsx`)
}

// 专利查询网站数据
const patentSites = [
  {
    id: 1,
    name: '中国国家知识产权局',
    url: 'https://cpquery.cponline.cnipa.gov.cn/chinesepatent/index',
    description: '中国及多国专利审查信息查询，提供权威的中国专利查询服务',
    icon: 'CN',
    color: '#de2910',
    recommended: false,
    region: 'domestic'
  },
  {
    id: 2,
    name: '美国专利商标局',
    url: 'https://www.uspto.gov/',
    description: '美国专利商标局官方查询网站，提供全面的美国专利信息',
    icon: 'US',
    color: '#3c3b6e',
    recommended: false,
    region: 'international'
  },
  {
    id: 3,
    name: '欧盟知识产权局',
    url: 'https://euipo.europa.eu/eSearch/',
    description: '欧盟专利查询平台，覆盖欧盟成员国的专利信息',
    icon: 'EU',
    color: '#003399',
    recommended: false,
    region: 'international'
  },
  {
    id: 4,
    name: '日本专利局',
    url: 'http://www.jpo.go.jp',
    description: '日本专利局官方网站，提供日本专利查询服务',
    icon: 'JP',
    color: '#bc002d',
    recommended: false,
    region: 'international'
  },
  {
    id: 5,
    name: '英国知识产权局',
    url: 'https://www.gov.uk/topic/intellectual-property',
    description: '英国官方知识产权查询网站，涵盖专利、商标等信息',
    icon: 'UK',
    color: '#012169',
    recommended: false,
    region: 'international'
  },
  {
    id: 6,
    name: '德国专利商标局',
    url: 'https://www.dpma.de/',
    description: '德国专利商标局官方查询平台，提供德国专利信息',
    icon: 'DE',
    color: '#dd0000',
    recommended: false,
    region: 'international'
  },
  {
    id: 7,
    name: '韩国专利局',
    url: 'https://www.patent.go.kr/smart/portal/Main.do',
    description: '韩国专利局官方网站，提供韩国专利查询服务',
    icon: 'KR',
    color: '#003478',
    recommended: false,
    region: 'international'
  },
  {
    id: 8,
    name: '澳大利亚知识产权局',
    url: 'https://www.ipaustralia.gov.au/',
    description: '澳大利亚官方知识产权查询网站，涵盖专利、商标等',
    icon: 'AU',
    color: '#012169',
    recommended: false,
    region: 'international'
  },
  {
    id: 9,
    name: '加拿大知识产权局',
    url: 'https://ised-isde.canada.ca/site/canadian-intellectual-property-office/en',
    description: '加拿大知识产权局官方查询平台，提供加拿大专利信息',
    icon: 'CA',
    color: '#ff0000',
    recommended: false,
    region: 'international'
  },
  {
    id: 10,
    name: '新西兰知识产权局',
    url: 'https://www.iponz.govt.nz/',
    description: '新西兰官方知识产权查询网站，提供专利查询服务',
    icon: 'NZ',
    color: '#012169',
    recommended: false,
    region: 'international'
  },
  {
    id: 11,
    name: '谷歌专利查询',
    url: 'https://patents.google.com/',
    description: '谷歌提供的全球专利搜索平台，覆盖全球专利文献',
    icon: 'G',
    color: '#4285f4',
    recommended: true,
    region: 'international'
  },
  {
    id: 12,
    name: 'SooPAT专利搜索',
    url: 'https://www.soopat.com/Patent/Index',
    description: '中文专利搜索引擎，提供便捷的专利查询服务',
    icon: 'S',
    color: '#4caf50',
    recommended: false,
    region: 'domestic'
  },
  {
    id: 13,
    name: '专利汇',
    url: 'https://www.patenthub.cn/help/system',
    description: '专业的专利查询与分析平台，提供深度专利分析',
    icon: 'P',
    color: '#9c27b0',
    recommended: false,
    region: 'domestic'
  },
  {
    id: 14,
    name: '智慧芽',
    url: 'https://www.zhihuiya.com/',
    description: '专业的专利查询与分析平台，特别适合比赛和科研使用',
    icon: 'Z',
    color: '#00a950',
    recommended: true,
    region: 'domestic'
  }
]

// 过滤网站列表
const filteredSites = computed(() => {
  let result = patentSites
  
  // 根据筛选类型过滤
  if (filterType.value !== 'all') {
    if (filterType.value === 'recommended') {
      result = result.filter(site => site.recommended)
    } else {
      result = result.filter(site => site.region === filterType.value)
    }
  }
  
  // 根据搜索文本过滤
  if (searchText.value) {
    const query = searchText.value.toLowerCase()
    result = result.filter(site => 
      site.name.toLowerCase().includes(query) || 
      site.description.toLowerCase().includes(query)
    )
  }
  
  return result
})

const formatUrl = (url) => {
  return url.replace(/^https?:\/\//, '').replace(/\/$/, '')
}

const visitSite = (url) => {
  window.open(url, '_blank')
}

getList()
</script>

<style scoped lang="scss">
.patent-sites-section {
  margin-top: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e4e7ed;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  border-left: 4px solid #409eff;
  padding-left: 10px;
}

.search-filter {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  align-items: center;
  flex-wrap: wrap;
}

.search-input {
  width: 300px;
}

.filter-group {
  flex: 1;
}

.patent-sites-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.site-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  height: 100%;
  border: 1px solid #e4e7ed;
}

.site-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
  border-color: #409eff;
}

.card-header {
  padding: 16px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #f0f2f5;
  background-color: #fafbfc;
}

.site-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  font-size: 16px;
  color: white;
  font-weight: bold;
}

.site-name {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.card-content {
  padding: 16px;
  flex-grow: 1;
}

.site-url {
  color: #409eff;
  font-size: 13px;
  margin-bottom: 10px;
  word-break: break-all;
  font-family: monospace;
}

.site-desc {
  color: #606266;
  line-height: 1.5;
  font-size: 14px;
}

.card-footer {
  padding: 12px 16px;
  background: #f5f7fa;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.visit-button {
  font-size: 13px;
}

.recommend-tag {
  background: #f56c6c;
  color: white;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 500;
}

@media (max-width: 1200px) {
  .patent-sites-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
}

@media (max-width: 768px) {
  .search-filter {
    flex-direction: column;
    align-items: stretch;
  }
  
  .search-input {
    width: 100%;
  }
  
  .patent-sites-grid {
    grid-template-columns: 1fr;
  }
}
</style>